﻿using System;

namespace HoNSharp
{
    class HoNRecvBuffer : GenericRecvBuffer
    {
        public bool HasPacket()
        {
            if (this._buffer.Length > 0)
            {
                if (this._buffer.Length > this.PeekWORD())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public UInt32 PeekDWORD()
        {
            return BitConverter.ToUInt32(this.stringToByteArray(this._buffer.Substring(_pos, 4)), 0);
        }

        public UInt16 PeekWORD()
        {
            byte[] len = this.stringToByteArray(this._buffer.Substring(_pos, 2));
            byte tmp = len[0];
            len[0] = len[1];
            len[1] = tmp;
            return BitConverter.ToUInt16(len, 0);
        }

        public void UpdateBuffer()
        {
            this._buffer = this._buffer.Substring(this._pos);
            this._pos = 0;
        }

        public string GetPacket()
        {
            return this._buffer.Substring(0, this.PeekWORD() + 2);
        }

        public byte[] GetPacketBytes()
        {
            return this.stringToByteArray(this._buffer.Substring(0, this.PeekWORD() + 2));
        }

        public UInt32 ExtractDWORD()
        {
            UInt32 retVal = BitConverter.ToUInt32(this.stringToByteArray(this._buffer.Substring(this._pos, 4)), 0);
            this._pos += 4;
            return retVal;
        }

        public UInt16 ExtractWORD()
        {
            UInt16 retVal = BitConverter.ToUInt16(this.stringToByteArray(this._buffer.Substring(this._pos, 2)), 0);
            this._pos += 2;
            return retVal;
        }

        public UInt16 ExtractBigEndianWORD()
        {
            byte[] len = this.stringToByteArray(this._buffer.Substring(_pos, 2));
            byte tmp = len[0];
            len[0] = len[1];
            len[1] = tmp;
            this._pos += 2;
            return BitConverter.ToUInt16(len, 0);
        }

        public Byte ExtractByte()
        {
            Byte retVal = Convert.ToByte(this._buffer[this._pos]);
            this._pos += 1;
            return retVal;
        }

        public byte[] ExtractBytes(int length)
        {
            byte[] data = new byte[length];
            data = this.stringToByteArray(this._buffer.Substring(this._pos, length));
            this._pos += length;
            return data;
        }

        public string ExtractString()
        {
            string ret = this._buffer.Substring(this._pos, this._buffer.IndexOf('\0', this._pos) - this._pos);
            this._pos += (ret.Length + 1);
            return ret;
        }

        public void Flush()
        {
            this._buffer = "";
            this._pos = 0;
        }
    }
}